<?php
/**
 * Bootstrapper functions and definitions.
 *
 * Sets up the theme and provides some helper functions, which are used in the
 * theme as custom template tags. Others are attached to action and filter
 * hooks in WordPress to change core functionality.
 *
 * When using a child theme (see http://codex.wordpress.org/Theme_Development
 * and http://codex.wordpress.org/Child_Themes), you can override certain
 * functions (those wrapped in a function_exists() call) by defining them first
 * in your child theme's functions.php file. The child theme's functions.php
 * file is included before the parent theme's file, so the child theme
 * functions would be used.
 *
 * Functions that are not pluggable (not wrapped in function_exists()) are
 * instead attached to a filter or action hook.
 *
 * For more information on hooks, actions, and filters,
 * see http://codex.wordpress.org/Plugin_API
 *
 * @package WordPress
 * @subpackage Bootstrapper
 * @since Bootstrapper 1.0
 */

/**
 * Sets up the content width value based on the theme's design.
 * @see bootstrapper_content_width() for template-specific adjustments.
 */
if ( ! isset( $content_width ) )
	$content_width = 604;

/**
 * Romove the WP admin bar from the frontend
 * @see bootstrapper_content_width() for template-specific adjustments.
 */
show_admin_bar( false );

/**
 * Bootstrapper only works in WordPress 3.6 or later.
 */
if ( version_compare( $GLOBALS['wp_version'], '3.6-alpha', '<' ) )
	require( get_template_directory() . '/inc/back-compat.php' );

/**
 * Sets up theme defaults and registers the various WordPress features that
 * Bootstrapper supports.
 *
 * @uses load_theme_textdomain() For translation/localization support.
 * @uses add_editor_style() To add a Visual Editor stylesheet.
 * @uses add_theme_support() To add support for automatic feed links, post
 * formats, and post thumbnails.
 * @uses register_nav_menu() To add support for a navigation menu.
 * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
 *
 * @since Bootstrapper 1.0
 *
 * @return void
 */
function bootstrapper_setup() {
	/*
	 * Makes Bootstrapper available for translation.
	 *
	 * Translations can be added to the /languages/ directory.
	 * If you're building a theme based on Bootstrapper, use a find and
	 * replace to change 'bootstrapper' to the name of your theme in all
	 * template files.
	 */
	load_theme_textdomain( 'bootstrapper', get_template_directory() . '/languages' );

	/*
	 * This theme styles the visual editor to resemble the theme style,
	 * specifically font, colors, and column width.
	 */
	add_editor_style( 'css/editor-style.css' );

	// Adds RSS feed links to <head> for posts and comments.
	add_theme_support( 'automatic-feed-links' );

	/*
	 * This theme supports all available post formats by default.
	 * See http://codex.wordpress.org/Post_Formats
	 *
     * For most formats, we don't need to explicitly state support. These include 
	 * Aside, Audio, Chat, Gallery, Image, Quote, Status, and Video. 
	 * 
	 * Structured post formats, however, are formats where the theme handles 
	 * the output instead of the default core HTML output. In the case of 
	 * Bootstrapper, only the structured Link format needs to be declared. 
	 * See bootstrapper_get_link_url()
	 */
	add_theme_support( 'structured-post-formats', array(
		'link'
	) );

	// This theme uses bootstrapper_nav() in one location.
	register_nav_menu( 'primary', __( 'Main Navigation Menu', 'bootstrapper' ) );

	/*
	 * This theme uses a custom image size for featured images, displayed on
	 * "standard" posts and pages.
	 */
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 604, 270, true );

	// Register custom image size for image post formats.
	add_image_size( 'bootstrapper-image-post', 724, 1288 );

	// This theme uses its own gallery styles.
	add_filter( 'use_default_gallery_style', '__return_false' );
}
add_action( 'after_setup_theme', 'bootstrapper_setup' );

/**
 * Returns the Google font stylesheet URL, if available.
 *
 * The use of Source Sans Pro and Bitter by default is localized. For languages
 * that use characters not supported by the font, the font can be disabled.
 *
 * @since Bootstrapper 1.0
 *
 * @return string Font stylesheet or empty string if disabled.
 */
function bootstrapper_fonts_url() {
	$fonts_url = '';

	/* Translators: If there are characters in your language that are not
	 * supported by Source Sans Pro, translate this to 'off'. Do not translate
	 * into your own language.
	 */
	$source_sans_pro = _x( 'on', 'Source Sans Pro font: on or off', 'bootstrapper' );

	/* Translators: If there are characters in your language that are not
	 * supported by Bitter, translate this to 'off'. Do not translate into your
	 * own language.
	 */
	$bitter = _x( 'on', 'Bitter font: on or off', 'bootstrapper' );

	if ( 'off' !== $source_sans_pro || 'off' !== $bitter ) {
		$font_families = array();

		if ( 'off' !== $source_sans_pro )
			$font_families[] = 'Source+Sans+Pro:400,700,300italic,400italic,700italic';

		if ( 'off' !== $bitter )
			$font_families[] = 'Bitter:400,700';

		$protocol = is_ssl() ? 'https' : 'http';
		$query_args = array(
			'family' => implode( '|', $font_families ),
			'subset' => 'latin,latin-ext',
		);
		$fonts_url = add_query_arg( $query_args, "$protocol://fonts.googleapis.com/css" );
	}

	return $fonts_url;
}

/**
 * Loads our special font CSS file.
 *
 * To disable in a child theme, use wp_dequeue_style()
 * function mytheme_dequeue_fonts() {
 *     wp_dequeue_style( 'bootstrapper-fonts' );
 * }
 * add_action( 'wp_enqueue_scripts', 'mytheme_dequeue_fonts', 11 );
 *
 * @since Bootstrapper 1.0
 *
 * @return void
 */
function bootstrapper_fonts() {
	$fonts_url = bootstrapper_fonts_url();
	if ( ! empty( $fonts_url ) )
		wp_enqueue_style( 'bootstrapper-fonts', esc_url_raw( $fonts_url ), array(), null );
}
add_action( 'wp_enqueue_scripts', 'bootstrapper_fonts' );

/**
 * Adds additional stylesheets to the TinyMCE editor if needed.
 *
 * @uses bootstrapper_fonts_url() to get the Google Font stylesheet URL.
 *
 * @since Bootstrapper 1.0
 *
 * @param string $mce_css CSS path to load in TinyMCE.
 * @return string
 */
function bootstrapper_mce_css( $mce_css ) {
	$fonts_url = bootstrapper_fonts_url();

	if ( empty( $fonts_url ) )
		return $mce_css;

	if ( ! empty( $mce_css ) )
		$mce_css .= ',';

	$mce_css .= esc_url_raw( str_replace( ',', '%2C', $fonts_url ) );

	return $mce_css;
}
add_filter( 'mce_css', 'bootstrapper_mce_css' );

/**
 * Enqueues scripts and styles for front end.
 *
 * @since Bootstrapper 1.0
 *
 * @return void
 */
function bootstrapper_scripts_styles() {
	global $wp_styles;

	// Loads Twitter Bootstrap stylesheets.
	wp_enqueue_style( 'bootstrap-base-style', get_template_directory_uri() . '/css/bootstrap.min.css', array(), '2.3.1' );
	wp_enqueue_style( 'bootstrap-responsive-style', get_template_directory_uri() . '/css/bootstrap-responsive.min.css', array( 'bootstrap-base-style' ), '2.3.1' );

	// Loads our theme stylesheet.
	wp_enqueue_style( 'bootstrapper-style', get_stylesheet_uri() );

	// Loads the Internet Explorer specific stylesheet.
	wp_enqueue_style( 'bootstrapper-ie', get_template_directory_uri() . '/css/ie.css', array( 'bootstrapper-style' ), '20130213' );
	$wp_styles->add_data( 'bootstrapper-ie', 'conditional', 'lt IE 9' );

	// Loads Twitter Bootstrap JavaScript.
	wp_enqueue_script( 'bootstrap-script', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ), '2.3.1', true );

	// Loads our theme JavaScript.
	wp_enqueue_script( 'bootstrapper-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20130416', true );
	wp_enqueue_script( 'bootstrapper-script', get_template_directory_uri() . '/js/ajax/ajax.js', array(), '1.0', true );

	//Adds JavaScript to pages with the comment form to support sites with threaded comments (when in use).
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );

	// Adds Masonry to handle vertical alignment of footer widgets.
	if ( is_active_sidebar( 'sidebar-1' ) )
		wp_enqueue_script( 'jquery-masonry' );
}
add_action( 'wp_enqueue_scripts', 'bootstrapper_scripts_styles' );

/**
 * Creates a nicely formatted and more specific title element text for output
 * in head of document, based on current view.
 *
 * @since Bootstrapper 1.0
 *
 * @param string $title Default title text for current view.
 * @param string $sep Optional separator.
 * @return string Filtered title.
 */
function bootstrapper_wp_title( $title, $sep ) {
	global $paged, $page;

	if ( is_feed() )
		return $title;

	// Add the site name.
	$title .= get_bloginfo( 'name' );

	// Add the site description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title = "$title $sep $site_description";

	// Add a page number if necessary.
	if ( $paged >= 2 || $page >= 2 )
		$title = "$title $sep " . sprintf( __( 'Page %s', 'bootstrapper' ), max( $paged, $page ) );

	return $title;
}
add_filter( 'wp_title', 'bootstrapper_wp_title', 10, 2 );

/**
 * Registers two widget areas.
 *
 * @since Bootstrapper 1.0
 *
 * @return void
 */
function bootstrapper_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Main Widget Area', 'bootstrapper' ),
		'id'            => 'primary',
		'description'   => __( 'Appears on posts and pages in the upper sidebar.', 'bootstrapper' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Secondary Widget Area', 'bootstrapper' ),
		'id'            => 'secondary',
		'description'   => __( 'Appears on posts and pages in the lower sidebar.', 'bootstrapper' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
}
add_action( 'widgets_init', 'bootstrapper_widgets_init' );

if ( ! function_exists( 'bootstrapper_nav' ) ) :
/**
 * Custom function to ouput nav menu html, since our responsive design will only support 2 levels of menu depth
 *
 * @since wpBootstrap 1.0
 * @param str $theme_location Menu location.
 */
function bootstrapper_nav( $theme_location ) {
	if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $theme_location ] ) ) {  // If the nav menu is registered,
		$menu = wp_get_nav_menu_object( $locations[ $theme_location ] );  // get the wp nav object,
		$menu_items = wp_get_nav_menu_items($menu->term_id);  // get array of nav items,
		_wp_menu_item_classes_by_context( $menu_items );  // set up the $menu_item variables.
		foreach ( $menu_items as $key => $menu_item ) :  // Sort each item in a new array,
			$sorted_menu_items[$menu_item->menu_order] = $menu_item;  // with item nav order as the key & item obj as the value.
			$menu_item_parents[$menu_item->menu_order] = $menu_item->menu_item_parent;  // with item nav order as the key & item parent as the value.
		endforeach;
		$navHtml .= '<ul class="nav">';  // Start the html
		foreach ( $sorted_menu_items as $menu_order => $nav_item ) :  // For each nav item,
			// Close the html for prvious items / Start subnavs
				$childIDs = array(); // reset array
				$itemHeader = false;
				if ( !isset($prevID) ) :  // First Nav Item
						$prevLV = 1;
						$childIDs = array_keys ( $menu_item_parents, $nav_item->ID );
				else :  // Not the first nav item, do check:
					if ( !empty($nav_item->menu_item_parent) ) :  // Is sub menu?
						if ( $prevID == $nav_item->menu_item_parent && $prevLV == 2 ) :  // Sub menu at 2 levels deep, make menu item header
							$itemHeader = true;
							$prevLV = 2;
						elseif ( $prevID == $nav_item->menu_item_parent && $prevLV == 1 ) :  // Start sub menu at 1 level deep?
							$navHtml .= '<ul class="dropdown-menu">';
							$prevLV = 2;
						else : // Else continue submenu.
							$navHtml .= "</li>";
							$prevLV = 2;
						endif;
					elseif ( $prevLV == 2 ) :  // close prev sub menu?
						$navHtml .= "</li></ul></li>";
						$prevLV = 1;
						$childIDs = array_keys ( $menu_item_parents, $nav_item->ID );
					else : // close prev top level.
						$navHtml .= "</li>";
						$prevLV = 1;
						$childIDs = array_keys ( $menu_item_parents, $nav_item->ID );
					endif;
				endif;  // !isset($prevID)
			// Set HTML Classes
				$itemClasses = 'nav-item';
				if ( $nav_item->current == true ) { $itemClasses .= ' active'; }
				if ( $nav_item->current_item_parent == true ) { $itemClasses .= ' active_parent'; }
				if ( $nav_item->current_item_ancestor == true ) { $itemClasses .= ' active_ancestor'; }
				if ( $nav_item->url =='#' || $itemHeader == true ) {
					$navHtml .= "<li class=\"divider\"></li><li class=\"nav-header\">$nav_item->title";
				} elseif ( !empty($childIDs) ) {
					$itemClasses .= ' dropdown';
					$navHtml .= "<li class=\"$itemClasses\"><a href=\"#\"  class=\"dropdown-toggle\" data-toggle=\"dropdown\">$nav_item->title <b class=\"caret\"></b></a>";
				} else {
					$navHtml .= "<li class=\"$itemClasses\"><a href=\"$nav_item->url\">$nav_item->title</a>";
				}
			$prevID = $nav_item->ID;
		endforeach;  // $sorted_menu_items as $menu_order => $nav_item
		if ( $prevLV == 1 ) { $navHtml .= "</li></ul>"; } else { $navHtml .= "</li></ul></li></ul>"; }  // Close the html
		echo $navHtml;  // Echo the Nav menu
	}
}
endif;

if ( ! function_exists( 'bootstrapper_user_quick_menu' ) ) :
/**
 * Custom function to output html for the User Quick Menu.
 * NOTE: if overriding this function in a child theme, also see the template /inc/ajax_posts/wp_login.php
 *
 * @since wpBootstrap 1.0
 */
function bootstrapper_user_quick_menu( $user = null ) {
	global $current_user;
	get_currentuserinfo();
	return '
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
            Welcome ' . $current_user->display_name . '
            <span id="userAvatar">' . get_avatar( $current_user->ID, 25 ) . '</span>
        </a>
        <ul class="dropdown-menu">
            <li><a href="' . wp_logout_url( get_permalink() ) . '" title="Logout">Logout</a></li>
        </ul>
    </li>
	';
}
endif;

if ( ! function_exists( 'bootstrapper_paging_nav' ) ) :
/**
 * Displays navigation to next/previous set of posts when applicable.
 *
 * @since Bootstrapper 1.0
 *
 * @return void
 */
function bootstrapper_paging_nav() {
	global $wp_query;

	// Don't print empty markup if there's only one page.
	if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
		return;
	?>
	<nav class="paging-navigation" role="navigation">
		<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'bootstrapper' ); ?></h1>
        <ul class="pager">
			<?php if ( get_next_posts_link() ) : ?>
                <li class="previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'bootstrapper' ) ); ?></li>
			<?php endif; ?>
			<?php if ( get_previous_posts_link() ) : ?>
                <li class="next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'bootstrapper' ) ); ?></li>
			<?php endif; ?>
        </ul>
	</nav><!-- /.paging-navigation -->
	<?php
}
endif;

if ( ! function_exists( 'bootstrapper_post_nav' ) ) :
/**
 * Displays navigation to next/previous post when applicable.
*
* @since Bootstrapper 1.0
*
* @return void
*/
function bootstrapper_post_nav() {
	global $post;

	// Don't print empty markup if there's nowhere to navigate.
	$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
	$next = get_adjacent_post( false, '', false );

	if ( ! $next && ! $previous )
		return;
	?>
	<nav class="post-navigation" role="navigation">
        <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'twentythirteen' ); ?></h1>
        <ul class="pager">
            <li class="previous">
                <?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'bootstrapper' ) . '</span> %title' ); ?>
            </li>
            <li class="next">
                <?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'bootstrapper' ) . '</span>' ); ?>
            </li>
        </ul>
	</nav><!-- .navigation -->
	<?php
}
endif;

if ( ! function_exists( 'bootstrapper_link_pages' ) ) :
/**
 * Custom function to ouput page links html, since our responsive design requires a <ul>. This is better :)
 *
 * @since Bootstrapper 1.0
 */

/**
 * The formatted output of a list of pages.
 *
 * Displays page links for paginated posts (i.e. includes the <!--nextpage-->.
 * Quicktag one or more times). This tag must be within The Loop.
 *
 * The defaults for overwriting are:
 * 'next_or_number' - Default is 'number' (string). Indicates whether page
 * numbers should be used. Valid values are number and next.
 * 'nextpagelink' - Default is 'Next Page' (string). Text for link to next page.
 * of the bookmark.
 * 'previouspagelink' - Default is 'Previous Page' (string). Text for link to
 * previous page, if available.
 * 'pagelink' - Default is '%' (String).Format string for page numbers. The % in
 * the parameter string will be replaced with the page number, so Page %
 * generates "Page 1", "Page 2", etc. Defaults to %, just the page number.
 * 'before' - Default is '<p> Pages:' (string). The html or text to prepend to
 * each bookmarks.
 * 'after' - Default is '</p>' (string). The html or text to append to each
 * bookmarks.
 * 'link_before' - Default is '' (string). The html or text to prepend to each
 * Pages link inside the <a> tag. Also prepended to the current item, which
 * is not linked.
 * 'link_after' - Default is '' (string). The html or text to append to each
 * Pages link inside the <a> tag. Also appended to the current item, which
 * is not linked.
 *
 * @param string|array $args Optional. Overwrite the defaults.
 * @return string Formatted output in HTML.
 */
function bootstrapper_link_pages($args = '') {
	$defaults = array(
			'before' => '<div class="page-navigation pagination pagination-small"><span><i class="icon-file"></i> Pages:</span><ul>',
			'after' => '</ul></div>',
			'link_before' => '',
			'link_after' => '',
			'nextpagelink' => __('Next'),
			'previouspagelink' => __('Prev'),
			'pagelink' => '%',
			'echo' => 1
	);
	$r = wp_parse_args( $args, $defaults );
	$r = apply_filters( 'wp_link_pages_args', $r );
	extract( $r, EXTR_SKIP );
	global $page, $numpages, $multipage, $more, $pagenow;
	$output = '';
	if ( $multipage ) {
		$output .= $before;
		$i = $page - 1;
		if ( $i && $more ) {
			$output .= _bootstrapper_link_page($i);
			$output .= $link_before . $previouspagelink . $link_after . '</a></li>';
		} else {
			$output .= '<li class="disabled"><a href="#">' . $previouspagelink . '</a></li>';
		}
		for ( $i = 1; $i < ($numpages+1); $i = $i + 1 ) {
			$j = str_replace('%',$i,$pagelink);
			$output .= ' ';
			if ( ($i != $page) || ((!$more) && ($page==1)) ) {
				$output .= _bootstrapper_link_page($i);
			} else {
				$output .= '<li class="active"><a href="#">';
			}
			$output .= $link_before . $j . $link_after;
			if ( ($i != $page) || ((!$more) && ($page==1)) ) {
					$output .= '</a></li>';
			} else {
				$output .= '</a></li>';
			}
		}
		$i = $page + 1;
		if ( $i <= $numpages && $more ) {
			$output .= _bootstrapper_link_page($i);
			$output .= $link_before . $nextpagelink . $link_after . '</a></li>';
		} else {
			$output .= '<li class="disabled"><a href="#">' . $nextpagelink . '</a></li>';
		}
		$output .= $after;
	}
	if ( $echo )
		echo $output;
	return $output;
}
endif;

if ( ! function_exists( '_bootstrapper_link_page' ) ) :
/**
 * Helper function for bootstrapper_link_pages().
 *
 * @since wpBootstrap 1.0
 *
 * @param int $i Page number.
 * @return string Link.
 */
function _bootstrapper_link_page( $i ) {
	global $wp_rewrite, $post;
	$post = get_post($post->ID);
	if ( 1 == $i ) {
		$url = get_permalink();
	} else {
		if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) )
			$url = add_query_arg( 'page', $i, get_permalink() );
		elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID )
			$url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged');
		else
			$url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged');
	}
	return '<li><a href="' . esc_url( $url ) . '">';
}
endif;

if ( ! function_exists( 'bootstrapper_entry_meta' ) ) :
/**
 * Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
 *
 * Create your own bootstrapper_entry_meta() to override in a child theme.
 *
 * @since Bootstrapper 1.0
 *
 * @return void
 */
function bootstrapper_entry_meta() {
	if ( is_sticky() && is_home() && ! is_paged() )
		echo '<span class="featured-post"><i class="icon-star"></i> <small class="muted">' . __( 'Sticky', 'bootstrapper' ) . '</small></span>';

	if ( ! has_post_format( 'aside' ) && ! has_post_format( 'link' ) && 'post' == get_post_type() )
		bootstrapper_entry_date();

	// Translators: used between list items, there is a space after the comma.
	$categories_list = get_the_category_list( __( ', ', 'bootstrapper' ) );
	if ( $categories_list ) {
		echo '<span class="categories-links"><i class="icon-folder-open"></i> <small class="muted">' . $categories_list . '</small></span>';
	}

	// Translators: used between list items, there is a space after the comma.
	$tag_list = get_the_tag_list( '', __( ', ', 'bootstrapper' ) );
	if ( $tag_list ) {
		echo '<span class="tags-links"><i class="icon-tags"></i> <small class="muted">' . $tag_list . '</small></span>';
	}

	// Post author
	if ( 'post' == get_post_type() ) {
		printf( '<span class="author vcard"><i class="icon-user"></i> <small class="muted"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></small></span>',
			esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
			esc_attr( sprintf( __( 'View all posts by %s', 'bootstrapper' ), get_the_author() ) ),
			get_the_author()
		);
	}
}
endif;

if ( ! function_exists( 'bootstrapper_entry_date' ) ) :
/**
 * Prints HTML with date information for current post.
 *
 * Create your own bootstrapper_entry_date() to override in a child theme.
 *
 * @since Bootstrapper 1.0
 *
 * @param boolean $echo Whether to echo the date. Default true.
 * @return string
 */
function bootstrapper_entry_date( $echo = true ) {
	$format_prefix = ( has_post_format( 'chat' ) || has_post_format( 'status' ) ) ? _x( '%1$s on %2$s', '1: post format name. 2: date', 'bootstrapper' ): '%2$s';

	$date = sprintf( '<span class="date"><i class="icon-calendar"></i> <small class="muted"> <a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a></small></span>',
		esc_url( get_permalink() ),
		esc_attr( sprintf( __( 'Permalink to %s', 'bootstrapper' ), the_title_attribute( 'echo=0' ) ) ),
		esc_attr( get_the_date( 'c' ) ),
		esc_html( sprintf( $format_prefix, get_post_format_string( get_post_format() ), get_the_date() ) )
	);

	if ( $echo )
		echo $date;

	return $date;
}
endif;

/**
 * Returns the URL from the post.
 *
 * @uses get_the_link() to get the URL in the post meta (if it exists) or
 * the first link found in the post content.
 *
 * Falls back to the post permalink if no URL is found in the post.
 *
 * @since Bootstrapper 1.0
 * @return string URL
 */
function bootstrapper_get_link_url() {
	$has_url = get_the_post_format_url();

	return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
}

/**
 * Sets the image size in featured galleries to large.
 *
 * @since Bootstrapper 1.0
 *
 * @param array $atts Combined and filtered attribute list.
 * @return array
 */
function bootstrapper_gallery_atts( $atts ) {
	if ( has_post_format( 'gallery' ) && ! is_single() )
		$atts['size'] = 'large';

	return $atts;
}
add_filter( 'shortcode_atts_gallery', 'bootstrapper_gallery_atts' );

/**
 * Extends the default WordPress body class to denote:
 * 1. Custom fonts enabled.
 * 2. Single or multiple authors.
 * 3. Active widgets in the sidebar to change the layout and spacing.
 * 4. When avatars are disabled in discussion settings.
 *
 * @since Bootstrapper 1.0
 *
 * @param array $classes Existing class values.
 * @return array Filtered class values.
 */
function bootstrapper_body_class( $classes ) {

	// Enable custom font class only if the font CSS is queued to load.
	if ( wp_style_is( 'bootstrapper-fonts', 'enqueued' ) )
		$classes[] = 'custom-font';

	if ( ! is_multi_author() )
		$classes[] = 'single-author';

	if ( is_active_sidebar( 'sidebar-2' ) && ! is_attachment() && ! is_404() )
		$classes[] = 'sidebar';

	if ( ! get_option( 'show_avatars' ) )
		$classes[] = 'no-avatars';

	return $classes;
}
add_filter( 'body_class', 'bootstrapper_body_class' );

/**
 * Adjusts content_width value for video post formats and attachment templates.
 *
 * @since Bootstrapper 1.0
 *
 * @return void
 */
function bootstrapper_content_width() {
	if ( has_post_format( 'video' ) || is_attachment() ) {
		global $content_width;
		$content_width = 724;
	}
}
add_action( 'template_redirect', 'bootstrapper_content_width' );

/**
 * Adjusts content_width value for video shortcodes in video post formats.
 *
 * @since Bootstrapper 1.0
 *
 * @param array $atts Attribute list.
 * @return array Filtered attribute list.
 */
function bootstrapper_video_width( $atts ) {
	if ( ! is_admin() && has_post_format( 'video' ) ) {
		$new_width = 724;
		$atts['height'] = round( ( $atts['height'] * $new_width ) / $atts['width'] );
		$atts['width'] = $new_width;
	}

	return $atts;
}
add_action( 'embed_defaults',       'bootstrapper_video_width' );
add_action( 'shortcode_atts_video', 'bootstrapper_video_width' );

/**
 * Adds entry date to aside posts after the content.
 *
 *
 * @since Bootstrapper 1.0
 *
 * @param string $content Post content.
 * @return string Post content.
 */
function bootstrapper_aside_date( $content ) {
	if ( ! is_feed() && has_post_format( 'aside' ) ) {
		$content .= bootstrapper_entry_date( false );
	}
	return $content;
}
add_filter( 'the_content', 'bootstrapper_aside_date', 8 ); // After embeds, before everything else.

/**
 * Switches default core markup for search form to output valid HTML5.
 *
 * @param string $format Expected markup format, default is `xhtml`
 * @return string Bootstrapper loves HTML5.
 */
function bootstrapper_search_form_format( $format ) {
	return 'html5';
}
add_filter( 'search_form_format', 'bootstrapper_search_form_format' );


/**
 * Adds ajax callback for the navbar login form
 *
 */
function bootstrapper_login_function() {
	check_ajax_referer( 'bootstrapper-login', 'nonce' );
	$creds = array(
		'user_login' => $_POST['user'],
		'user_password' => $_POST['pass'],
		'remember' => true
	);
	$user = wp_signon( $creds, false);
	if ( is_wp_error($user) )
		echo json_encode( array( 'success' => false ) );	   
	else 
		echo json_encode( array( 'success' => true ) );	   
	die(); // this is required to return a proper result
}
add_action( 'wp_ajax_nopriv_bootstrapper_login', 'bootstrapper_login_function' );


function bootstrapper_login_script() {
	?>
	<script type="text/javascript">
		jQuery(document).ready(function($){
		   $("#bootstrapper_login_form").submit(function() {
				var data = {
					action: 'bootstrapper_login',
					nonce: '<?php echo wp_create_nonce("bootstrapper-login"); ?>',
					user: $('#bootstrapper_login_form [name="log"]').val(),
					pass: $('#bootstrapper_login_form [name="pwd"]').val()
				};
				$.post("<?php echo admin_url('admin-ajax.php'); ?>", data, function(result) {
					var response = jQuery.parseJSON(result);  // Parse response
					if ( response.success == true ) {
						window.location.reload();
					} else {
						window.location = '<?php echo site_url('login'); ?>';
					}
				});
				return false;
			});
		});
	</script>
	<?php
}
add_action( 'wp_footer', 'bootstrapper_login_script' );




/** --------------------------------------------------------------------- **/
/** ---------------------  AZ RENT TO OWN SPECIFIC  --------------------- **/
/** --------------------------------------------------------------------- **/




/**
 * Drop in a custom logo into the WordPress administration
 *
 * @since Bootstrapper 1.0
 */
function admin_custom_logo() {
   echo '<style type="text/css">#wp-admin-bar-wp-logo .ab-icon { height: 22px !important; margin-top: 3px !important; background-image: url(' . get_bloginfo('template_directory') . '/img/logo_azrenttoown_mini.png) !important; background-position: 0 0 !important; }</style>';
}
add_action('admin_bar_menu', 'admin_custom_logo');













